<?php
$back = $_POST["formular_url"]."?";
$blacklist=array("next","formular_url","submit","error","file_maxsize");
foreach($_POST as $key_name => $key_value) {
  if (in_array($key_name, $blacklist)) continue;
  $key_value = str_replace(chr(10),"<br/>",$key_value);
  $key_value = str_replace(chr(13),"",$key_value);
  $back .= $key_name . "=". utf8decode($key_value) . "&";
}
if (strlen($_POST['name']) < 3)
{ 
    $back .= "error=Mind. 3 chars!";
    header("Location: $back");    exit();
}
$pattern = "/^[A-z0-9\._-]+@[A-z0-9][A-z0-9-]*(\.[A-z0-9_-]+)*\.([A-z]{2,6})$/";
if (! preg_match($pattern, $_POST['mail']))
{ 
    $back .= "error=Not a valid Email-Adress";
    header("Location: $back");    exit();
}
if ($_POST['submit'] == true) {  
$date=date('d.m.Y');  
$time=date('H,i,s');  
$receipt='your@adress.de'; 
$next="http://localhost/doku.php?id=intro:formdoku"; 
$string="Empfangen von $date : $time: " . "\n dokupage ==> " . $_POST['dokupage'] 
 . "\n name ==> " . $_POST['name'] 
 . "\n mail ==> " . $_POST['mail'] 
 . "\n news ==> " . $_POST['news'] 
 . "\n comments ==> \" " . $_POST['comments'] . " \" " 
;  
$string .= "\nSender: ".$_SERVER['LOGON_USER'].$_SERVER['REMOTE_USER']; 
$boundary = strtoupper(md5(uniqid(time())));
$mail_header  = "From: Formular-Plugin <formular-plugin@dokuwiki>\n";
$mail_header .= "MIME-Version: 1.0";
$mail_header .= "\nContent-Type: multipart/mixed; boundary=$boundary";
$mail_header .= "\n\nThis is a multi-part message in MIME format  --  Dies ist eine mehrteilige Nachricht im MIME-Format";
$mail_header .= "\n--$boundary";
$mail_header .= "\nContent-Type: text/plain";
$mail_header .= "\nContent-Transfer-Encoding: 8bit";
$mail_header .= "\n\n$string";
$i = 0;
while (is_uploaded_file($_FILES['file']['tmp_name']['formular'][$i])) {
    if ($_FILES['file']['size']['formular'][$i] > ($_POST['file_maxsize'] * 1000000)) {
        $back .= "error=Die angegebene Datei ist zu groß";
        header("Location: $back");
        exit();
    }
    $file_content = fread(fopen($_FILES['file']['tmp_name']['formular'][$i],"r"),$_FILES['file']['size']['formular'][$i]);
    //$file_content = readfile($_FILES['file']['tmp_name']['formular'][$i]);
    $file_content = chunk_split(base64_encode($file_content));
    $mail_header .= "\n--$boundary";
    $mail_header .= "\nContent-Type: ".$_FILES['file']['type']['formular'][$i]."; name=\"".$_FILES['file']['name']['formular'][$i]."\"";
    $mail_header .= "\nContent-Transfer-Encoding: base64";
    $mail_header .= "\nContent-Disposition: attachment; filename=\"".$_FILES['file']['name']['formular'][$i]."\"";
    $mail_header .= "\n\n$file_content";
    $i++;
}
$mail_header .= "\n--$boundary--";
mail($receipt, 'Message - formular plugin', "", $mail_header); 
header("Location: $next");  
 } 
 ?>